package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.NetWorkVersionAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 二网通分为标准版和增强版，三网通为增强版
 */
@Service
@AttrHandler(NetWorkVersionAttrHandler.class)
public class NetWorkVersionAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 型号只有标准版
        KiteAttr modelAttr = inst.getAttr("200002772");//型号
        if (Objects.nonNull(modelAttr)) {
            // 如果是二网通
            if ("110007214".equals(attr.getValue())) {
                List<AttrValue> modelAttrValues = AttrValueCache.get(modelAttr.getAttrId());//"110009600"
                modelAttrValues = modelAttrValues.stream().filter(mAttr -> KiteStringUtils.equalsAny(mAttr.getAttrValue(),"110007342","110009600")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(modelAttrValues)) {
                    // 默认取第一个
                    AttrValue firstAttr = modelAttrValues.get(0);
                    modelAttr.setValue(firstAttr.getAttrValue());
                    modelAttr.setValueDesc(firstAttr.getAttrValueDesc());
                    modelAttr.setAttrValues(KiteBeanUtils.copyPropsForList(modelAttrValues, KiteAttrValue.class));
                    operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), modelAttr);
                    // 联动修改了型号，则触发一次型号的联动处理
                    SpringUtil.getBean(Cat1ModelAttrLinkHandler.class).handle(modelAttr, inst, data);

                    // 服务能力
                    KiteAttr abilityAttr = inst.getAttr("210004316");
                    if (Objects.nonNull(abilityAttr)) {
                        // 二网通标准版包括基础服务、基础服务+miniPCIe
                        if (KiteStringUtils.equals(firstAttr.getAttrValue(), "110007342")) {
                            List<AttrValue> abilityAttrValues = AttrValueCache.get(abilityAttr.getAttrId());
                            abilityAttrValues = abilityAttrValues.stream().filter(abilityAttrValue -> KiteStringUtils.equalsAny(abilityAttrValue.getAttrValue(), "110007347", "110007348")).collect(Collectors.toList());
                            if (KiteListUtils.isNotEmpty(abilityAttrValues)) {
                                // 默认取第一个
                                AttrValue abilityAttrValue = abilityAttrValues.get(0);
                                abilityAttr.setValue(abilityAttrValue.getAttrValue());
                                abilityAttr.setValueDesc(abilityAttrValue.getAttrValueDesc());
                                abilityAttr.setAttrValues(KiteBeanUtils.copyPropsForList(abilityAttrValues, KiteAttrValue.class));
                                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), abilityAttr);
                            }
                        }
                    }
                }
            }
            // 三网通
            else if ("110007215".equals(attr.getValue())) {
                List<AttrValue> modelAttrValues = AttrValueCache.get(modelAttr.getAttrId());
                modelAttrValues = modelAttrValues.stream().filter(mAttr -> !KiteStringUtils.equalsAny(mAttr.getAttrValue(),"110007342","110009600")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(modelAttrValues)) {
                    // 默认取第一个
                    AttrValue firstAttr = modelAttrValues.get(0);
                    modelAttr.setValue(firstAttr.getAttrValue());
                    modelAttr.setValueDesc(firstAttr.getAttrValueDesc());
                    modelAttr.setAttrValues(KiteBeanUtils.copyPropsForList(modelAttrValues, KiteAttrValue.class));
                    operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), modelAttr);
                    // 联动修改了型号，则触发一次型号的联动处理
                    SpringUtil.getBean(Cat1ModelAttrLinkHandler.class).handle(modelAttr, inst, data);

                    // 服务能力
                    KiteAttr abilityAttr = inst.getAttr("210004316");
                    if (Objects.nonNull(abilityAttr)) {
                        // 三网通增强版只有基础服务+miniPCIE版本
                        if (KiteStringUtils.equalsAny(firstAttr.getAttrValue(), "110007343", "110007344", "110007345", "110007346")) {
                            List<AttrValue> abilityAttrValues = AttrValueCache.get(abilityAttr.getAttrId());
                            abilityAttrValues = abilityAttrValues.stream().filter(abilityAttrValue -> KiteStringUtils.equalsAny(abilityAttrValue.getAttrValue(), "110007348")).collect(Collectors.toList());
                            if (KiteListUtils.isNotEmpty(abilityAttrValues)) {
                                // 默认取第一个
                                AttrValue abilityAttrValue = abilityAttrValues.get(0);
                                abilityAttr.setValue(abilityAttrValue.getAttrValue());
                                abilityAttr.setValueDesc(abilityAttrValue.getAttrValueDesc());
                                abilityAttr.setAttrValues(KiteBeanUtils.copyPropsForList(abilityAttrValues, KiteAttrValue.class));
                                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), abilityAttr);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
